#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<unistd.h>
#define __USE_GNU
#include<sched.h>
#include<pthread.h>

#define MAX_N   1000000

long    arr[MAX_N];

typedef struct 
{
    long start;
    long end;
    long * arr;
} Struct;

void * sum_func(void * input){
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(0,&cpuset);
    sched_setaffinity(0,sizeof(cpuset),&cpuset);
    Struct * array = input;
    long * res = malloc(sizeof(long));

    *res = 0;
    for(long i = array->start; i <= array->end;i+=2){
        (*res) += array->arr[i];
    }
    return res;
}


int main(){
    struct timeval tv1,tv2;
    int sum1 = 0,timelen = 0,i = 0;
    gettimeofday(&tv1,NULL);
    for(i = 0; i < MAX_N; i++){
        arr[i] = i+1;
    }
    i = 0;
    Struct odd_array = {0,MAX_N,arr};
    Struct even_array = {1,MAX_N,arr};
    void * odd_addr;
    void * even_addr;
    long odd_sum,even_sum,sum;

    pthread_t   add_thread_1,add_thread_2;
    pthread_create(&add_thread_1,NULL,sum_func,&odd_array);
    pthread_create(&add_thread_2,NULL,sum_func,&even_array);
    //odd_sum = *(long *)addr;
    //even_sum = 
    pthread_join(add_thread_1,&odd_addr);
    pthread_join(add_thread_2,&even_addr);
    gettimeofday(&tv2,NULL);
    odd_sum = *(long *)odd_addr;
    even_sum = *(long *)even_addr;
    sum = odd_sum + even_sum;
    printf("sum of array is:%ld\ntime length is:%d us\n",sum,tv2.tv_usec-tv1.tv_usec);
}